<?php

namespace App\Enums\Workflow;

use App\Models\OpenShop\Common\OpenShopOrderManagerModel;

class WorkflowV2Enum
{
    // 审批流类型
    const WORKFLOW_TYPE_WORK_ORDER = 1;

    const WORKFLOW_TYPE_TEXT = [
        self::WORKFLOW_TYPE_WORK_ORDER => '工单类型',
    ];


    // 节点类型
    const NODE_TYPE_SEQUENCE  = 1;
    const NODE_TYPE_AND       = 2;
    const NODE_TYPE_OR        = 3;
    const NODE_TYPE_IF_MAIN   = 4;
    const NODE_TYPE_IF_BRANCH = 5;
    const NODE_TYPE_CC        = 6;

    const NODE_TYPE_TEXT = [
        self::NODE_TYPE_SEQUENCE  => '顺序',
        self::NODE_TYPE_AND       => '会签',
        self::NODE_TYPE_OR        => '或签',
        self::NODE_TYPE_IF_MAIN   => '条件父',
        self::NODE_TYPE_IF_BRANCH => '条件分支',
        self::NODE_TYPE_CC        => '抄送',
    ];


    // 处理人类型
    const HANDLER_TYPE_HEAD         = 1;
    const HANDLER_TYPE_SUPERVISE    = 2;
    const HANDLER_TYPE_PROVINCE     = 3;
    const HANDLER_TYPE_MERCHANT     = 4;
    const HANDLER_TYPE_SHOP_MANAGER = 5;
    const HANDLER_TYPE_SUBMITTER    = 6;

    const HANDLER_TYPE_TEXT = [
        0                               => '无',
        self::HANDLER_TYPE_HEAD         => '总部',
        self::HANDLER_TYPE_SUPERVISE    => '督导',
        self::HANDLER_TYPE_PROVINCE     => '省总',
        self::HANDLER_TYPE_MERCHANT     => '加盟商本人',
        self::HANDLER_TYPE_SHOP_MANAGER => '门店店长',
        self::HANDLER_TYPE_SUBMITTER    => '提交人',
    ];

    const BACK_APPROVAL_TYPE_MAP = [
        self::HANDLER_TYPE_HEAD,
        self::HANDLER_TYPE_SUPERVISE,
        self::HANDLER_TYPE_PROVINCE,
    ];

    // 业务类型
    const BUSINESS_TYPE_0 = 0;
    const BUSINESS_TYPE_SUBMIT_FORM = 1;

    const BUSINESS_TYPE_TEXT = [
        self::BUSINESS_TYPE_0 => '无',
        self::BUSINESS_TYPE_SUBMIT_FORM => '表单提交',
    ];

    // open_shop_order_manager.type => handler_type
    const OPEN_SHOP_MANAGER_TYPE_TO_HANDLER_TYPE = [
        OpenShopOrderManagerModel::MANAGER_TYPE_5 => self::HANDLER_TYPE_SUPERVISE,
    ];

    // 节点状态
    const NODE_STATUS_WAIT  = 0;
    const NODE_STATUS_ING   = 1;
    const NODE_STATUS_OK    = 2;
    const NODE_STATUS_CHILD = 3;

    const STATUS_TEXT = [
        self::NODE_STATUS_WAIT  => '未开始',
        self::NODE_STATUS_ING   => '进行中',
        self::NODE_STATUS_OK    => '已完成',
        self::NODE_STATUS_CHILD => '等待子节点审批',
    ];

    // 条件公式formula
    const FORMULA_EQUALS   = '=';
    const FORMULA_GT       = '>';
    const FORMULA_GT_EQ    = '>=';
    const FORMULA_LT       = '<';
    const FORMULA_LT_EQ    = '<=';
    const FORMULA_UNEQUALS = '!=';
    const FORMULA_IN       = 'in';
    const FORMULA_NOT_IN   = 'not in';

    const FORMULA_TEXT = [
        self::FORMULA_EQUALS   => '等于',
        self::FORMULA_GT       => '大于',
        self::FORMULA_GT_EQ    => '大于等于',
        self::FORMULA_LT       => '小于',
        self::FORMULA_LT_EQ    => '小于等于',
        self::FORMULA_UNEQUALS => '不等于',
        self::FORMULA_IN       => '包含',
        self::FORMULA_NOT_IN   => '不包含',
    ];

    // 抄送人
    const CC_TYPE_KF_USER   = 1;
    const CC_TYPE_FS_GROUP  = 2;
    const CC_TYPE_SHOP_BOSS = 3;
    const CC_TYPE_SUBMITTER = 4;

    const CC_TYPE_TEXT = [
        self::CC_TYPE_KF_USER   => '飞书总部人员',
        self::CC_TYPE_FS_GROUP  => '飞书群组',
        self::CC_TYPE_SHOP_BOSS => '门店加盟商',
        self::CC_TYPE_SUBMITTER => '提交人',
    ];

    public static function formatEnums(string $field, array $enums): array
    {
        return [
            'field' => $field,
            'enums' => collect($enums)->map(function ($v, $k) {
                return [
                    'text'  => $v,
                    'value' => $k,
                ];
            })->values()->toArray(),
        ];
    }

    /**
     * 获取所有enums
     * @return array
     */
    public static function getEnums(): array
    {
        return [
            self::formatEnums('nodeType', self::NODE_TYPE_TEXT),
            self::formatEnums('businessType', self::BUSINESS_TYPE_TEXT),
            self::formatEnums('handlerType', self::HANDLER_TYPE_TEXT),
            self::formatEnums('formula', self::FORMULA_TEXT),
            self::formatEnums('ccType', self::CC_TYPE_TEXT),
        ];
    }
}
